package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwarT;
import cn.e4j.modular.crm.entity.GwarlT;
import cn.e4j.modular.crm.mapper.GwarTMapper;
import cn.e4j.modular.crm.service.GwarTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *   生命周期 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-11
 */
@Service
public class GwarTServiceImpl extends ServiceImpl<GwarTMapper, GwarT> implements GwarTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwarlist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwarDtos = this.baseMapper.listGwarPage(page, dto);

        return gwarDtos;
    }

    @Transactional(readOnly = true)
    public GwarT getByGwar001(String ent, String gwar001) {
        UpdateWrapper<GwarT> gwarTUpdateWrapper = new UpdateWrapper<>();
        gwarTUpdateWrapper.eq("GWARENT",ent);
        gwarTUpdateWrapper.eq("GWAR001",gwar001);
        GwarT gwar = this.baseMapper.selectOne(gwarTUpdateWrapper);
        return gwar;
    }

    @Transactional
    @Override
    public void add(GwarT gwar,GwarlT gwarl) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwar.setGWARENT(shiroUser.getDBC());
        gwar.setGWAR002(" ");
        gwar.setGWAR003(" ");//预留栏位2
        gwar.setGWARUSER(shiroUser.getACCOUNT());
        gwar.setGWARGRUP(shiroUser.getORG_ID());
        gwar.setGWARDATE(LocalDate.now());
        gwar.setGWARTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwar.setGWARORIG(shiroUser.getORG_ID());
        gwar.setGWARORIU(shiroUser.getACCOUNT());

        gwarl.setGWARLENT(shiroUser.getDBC());
        gwarl.setGWARL001(gwar.getGWAR001());
        gwarl.setGWARL002(LangUtils.getLang(shiroUser.getLANG()));
        gwar.insert();
        gwarl.insert();
    }

    @Transactional
    @Override
    public void edit(GwarT gwar,GwarlT gwarl) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwar.setGWARMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwarT> gwarTUpdateWrapper = new UpdateWrapper<>();
        gwarTUpdateWrapper.eq("GWARENT",gwar.getGWARENT());
        gwarTUpdateWrapper.eq("GWAR001",gwar.getGWAR001());

        UpdateWrapper<GwarlT> gwarlTUpdateWrapper = new UpdateWrapper<>();
        gwarlTUpdateWrapper.eq("GWARLENT",gwar.getGWARENT());
        gwarlTUpdateWrapper.eq("GWARL001",gwar.getGWAR001());

        gwar.update(gwarTUpdateWrapper);
        gwarl.update(gwarlTUpdateWrapper);
    }


    @Transactional
    @Override
    public void delete(GwarT gwar,GwarlT gwarl) {
        UpdateWrapper<GwarT> gwarTUpdateWrapper = new UpdateWrapper<>();
        gwarTUpdateWrapper.eq("GWARENT",gwar.getGWARENT());
        gwarTUpdateWrapper.eq("GWAR001",gwar.getGWAR001());

        UpdateWrapper<GwarlT> gwarlTUpdateWrapper = new UpdateWrapper<>();
        gwarlTUpdateWrapper.eq("GWARLENT",gwar.getGWARENT());
        gwarlTUpdateWrapper.eq("GWARL001",gwar.getGWAR001());

        gwar.delete(gwarTUpdateWrapper);
        gwarl.delete(gwarlTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwarT> gwarTList) {
        for(GwarT gwarT : gwarTList){
            UpdateWrapper<GwarT> gwarTUpdateWrapper = new UpdateWrapper<>();
            gwarTUpdateWrapper.eq("GWARENT",gwarT.getGWARENT());
            gwarTUpdateWrapper.eq("GWAR001",gwarT.getGWAR001());

            GwarlT gwarlT = new GwarlT();
            UpdateWrapper<GwarlT> gwarlTUpdateWrapper = new UpdateWrapper<>();
            gwarlTUpdateWrapper.eq("GWARLENT",gwarT.getGWARENT());
            gwarlTUpdateWrapper.eq("GWARL001",gwarT.getGWAR001());

            gwarT.delete(gwarTUpdateWrapper);
            gwarlT.delete(gwarlTUpdateWrapper);
        }
    }
}
